﻿using System;
using System.Text;

public class ReverseDigits
{
    public static void Main()
    {
        Common.Print(string.Format("Reverse number = {0}\n", Reverse(Common.InputDecimal("\nNumber = ").ToString())));
    }

    public static string Reverse(string number)
    {
        StringBuilder reverseNumber = new StringBuilder();
        string sign = "";
        int start = 0;

        if (number[0] == '-')
        {
            sign = "-";
            number = number.Remove(0, 1);
        }

        number = RemoveEmptyDecimal(number);

        for (int i = number.Length - 1; i >= 0; i--)
        {
            if (number[start] == '.')
            {
                reverseNumber.Append(".");
                if (number[i] == '.')
                    i--;

                i++;
            }
            else
            {
                if (number[i] == '.')
                {
                    if (number[start] == '.')
                        start++;

                    start--;
                }
                else
                    reverseNumber.Append(number[i]);
            }

            start++;
        }

        return sign + RemoveUnsignificantZeroes(reverseNumber.ToString());
    }

    public static string RemoveEmptyDecimal(string number)
    {
        int dotPosition = number.IndexOf('.');

        if (dotPosition != -1)
            for (int i = number.Length - 1; i > dotPosition; i--)
                if (number[i] != '0')
                {
                    if (i < number.Length - 1)
                        number = number.Remove(i + 1);

                    break;
                }

        return number;
    }

    public static string RemoveUnsignificantZeroes(string numberString)
    {
        int firstSignificant = 0;

        numberString = RemoveEmptyDecimal(numberString);

        if (numberString[0] == '0')
        {
            firstSignificant++;
            for (int o = 1; o < numberString.Length; o++)
            {
                if (numberString[o] != '0')
                    break;

                firstSignificant++;
            }
        }

        if (firstSignificant == numberString.Length)
            numberString = "0";
        else
        {
            numberString = numberString.Remove(0, firstSignificant);

            if (numberString[0] == '.')
                numberString = "0" + numberString;

            if (numberString[numberString.Length - 1] == '.')
                numberString = numberString.Remove(numberString.Length - 1);
        }

        return numberString;
    }
}